﻿using Newtonsoft.Json;
using Sustainalytics.ClientDW.DataAccessLayer;
using Sustainalytics.DataLayer;
using Sustainalytics.Entities;
using Sustainalytics.Replication.Publisher;
using Sustainalytics.ScreeningTool.BL.Model;
using Sustainalytics.ScreeningTool.BL.Serilog;
using System;
using System.Collections.Generic;
using System.Text;

namespace Sustainalytics.ScreeningTool.BL
{
    public class FilterCriteriaManager : IFilterCriteriaManager
    {
        private ICompanyScreeningRepository _companyScreeningRepository;
        private IClientDwRepository _clientDWRepository;
        private IFilterCriteriaRepository _filterCriteriaRepository;
        private ServiceBusPublisher _serviceBusPublisher;

        private FilterCriteriaManager()
        {
        }

        public static IFilterCriteriaManager GetInstance(
            ICompanyScreeningRepository companyScreeningRepository,
            IClientDwRepository clientDWRepository,
            IFilterCriteriaRepository filterCriteriaRepository,
            ServiceBusPublisher serviceBusPublisher)
        {
            return new FilterCriteriaManager
            {
                _companyScreeningRepository = companyScreeningRepository,
                _clientDWRepository = clientDWRepository,
                _filterCriteriaRepository = filterCriteriaRepository,
                _serviceBusPublisher = serviceBusPublisher
            };
        }

        #region Add Criteria to filter

        public FilterCriteria AddAndUpdateCriteriaTypeOne(Guid templateCriteriaId, Guid filterId, bool isReport, int? minValue, int? maxValue)
        {
            TemplateCriteria templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            FilterCriteria filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteria.Id);
            int lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }
            else
            {
                FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, null, isReport, templateCriteria, lastCountForFilterCriteria);

                newFilterCriteria.SelectedValue = isReport || !templateCriteria.IsScreenable ? null : JsonConvert.SerializeObject(new SelectedValueMinMax
                {
                    MinValue = minValue.Value,
                    MaxValue = maxValue.Value
                });
                newFilterCriteria.SelectedValueInformation = isReport || !templateCriteria.IsScreenable ? templateCriteria.Name : this.GetSelectedValueInformationForChartCriteria(minValue.Value, maxValue.Value);

                _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
                return newFilterCriteria;
            }
        }

        public FilterCriteria AddAndUpdateCriteriaTypeTwo(Guid templateCriteriaId, Guid filterId, Guid filterGroupId, bool isReport, int? score)
        {
            TemplateCriteria templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            FilterCriteria filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteria.Id);
            int lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }
            else
            {
                FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, filterGroupId, isReport, templateCriteria, lastCountForFilterCriteria);
                newFilterCriteria.SelectedValue = score != null ? JsonConvert.SerializeObject(new SelectedValueScore
                {
                    Score = score.Value
                }) : null;
                newFilterCriteria.SelectedValueInformation = score != null ? this.GetSelectedValueInformationForCriteriaType2(score) : templateCriteria.Name;

                _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
                return newFilterCriteria;
            }
        }


        public FilterCriteria AddAndUpdateCriteriaTypeThree(Guid templateCriteriaId, Guid filterId, bool isReport, List<string> selectedItems, Guid filterGroupId)
        {
            TemplateCriteria templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            FilterCriteria filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteria.Id);
            int lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }
            else
            {
                FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, filterGroupId, isReport, templateCriteria, lastCountForFilterCriteria);

                newFilterCriteria.SelectedValue = isReport || !templateCriteria.IsScreenable ? null : JsonConvert.SerializeObject(new SelectedValueSelectedItems
                {
                    SelectedItems = selectedItems
                });
                newFilterCriteria.SelectedValueInformation = isReport || !templateCriteria.IsScreenable ? templateCriteria.Name : this.GetSelectedValueInformationForCriteriaType3(selectedItems);

                _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
                return newFilterCriteria;
            }
        }

        public FilterCriteria AddAndUpdateCriteriaTypeFour(Guid templateCriteriaId, Guid filterId, bool isReport, string categoryOfInvolvement, Guid filterGroupId,
                string categorySymbol, bool? involvement, bool? significantOwnership, bool? revenueRange, double? piRangeStart, double? piRangeStop)
        {
            TemplateCriteria templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            FilterCriteria filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteriaId);
            int lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }
            else
            {
                //insert
                bool hasSelectedValue = involvement != null ? true : false;
                FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, filterGroupId, isReport, templateCriteria, lastCountForFilterCriteria);

                newFilterCriteria.SelectedValue = isReport || !hasSelectedValue ? null : JsonConvert.SerializeObject(new CriteriaTypeFour
                {
                    Name = templateCriteria.Name,
                    PiFilterCriteriaRange = new PIFilterCriteriaRange()
                    {
                        CategoryOfInvolvement = categoryOfInvolvement,
                        IsInvolvement = involvement.Value,
                        RevenueRange = revenueRange.Value,
                        IsSignificantOwnership = significantOwnership.Value,
                        Start = piRangeStart.Value,
                        Stop = piRangeStop.Value,
                        CategorySymbol = categorySymbol
                    },
                });
                newFilterCriteria.SelectedValueInformation = isReport || !hasSelectedValue ? 
                    templateCriteria.Name : 
                    GetSelectedValueInformationForCriteriaType4(
                        categoryOfInvolvement, 
                        involvement.Value, 
                        significantOwnership.Value, 
                        revenueRange.Value, 
                        piRangeStart.Value,
                        templateCriteria.ResearchProducts);

                _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
                return newFilterCriteria;
            }
        }

        public FilterCriteria AddAndUpdateCriteriaTypeFive(Guid templateCriteriaId, Guid filterId, Guid filterGroupId, bool isReport, int? score)
        {
            TemplateCriteria templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            FilterCriteria filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteria.Id);
            int lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);
            string templateIndicatorNumber = templateCriteria.Name.Split(' ')[0];

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }
            else
            {
                //insert
                FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, filterGroupId, isReport, templateCriteria, lastCountForFilterCriteria);

                newFilterCriteria.SelectedValue = score != null ? JsonConvert.SerializeObject(new SelectedValueScore
                {
                    Score = score.Value
                }) : null;
                newFilterCriteria.SelectedValueInformation = score != null ? _clientDWRepository.GetAnswerCategoryText(templateIndicatorNumber, score.Value) : templateCriteria.Name;

                _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
                return newFilterCriteria;
            }
        }

        public FilterCriteria AddAndUpdateCriteriaType25(Guid templateCriteriaId, Guid filterId, Guid filterGroupId, bool isReport, int? score)
        {
            TemplateCriteria templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            FilterCriteria filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteria.Id);
            int lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }
            else
            {
                //insert
                FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, filterGroupId, isReport, templateCriteria, lastCountForFilterCriteria);

                newFilterCriteria.SelectedValue = score != null ? JsonConvert.SerializeObject(new SelectedValueScore
                {
                    Score = score.Value
                }) : null;
                newFilterCriteria.SelectedValueInformation = score != null ? _companyScreeningRepository.GetRiskRatingAnswerCategoryText(templateCriteria, score.Value) : templateCriteria.Name;

                _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
                return newFilterCriteria;
            }
        }

        public FilterCriteria AddAndUpdateCriteriaTypeSix(Guid templateCriteriaId, Guid filterId, Guid filterGroupId, bool isReport, int? minValue, int? maxValue)
        {
            var templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            var filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteria.Id);
            var lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }

            //insert
            FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, filterGroupId, isReport, templateCriteria, lastCountForFilterCriteria);

            newFilterCriteria.SelectedValue = isReport || !templateCriteria.IsScreenable ? null : JsonConvert.SerializeObject(new SelectedValueMinMax
            {
                MinValue = minValue.Value,
                MaxValue = maxValue.Value
            });
            newFilterCriteria.SelectedValueInformation = isReport || !templateCriteria.IsScreenable ? templateCriteria.Name : GetSelectedValueInformationForChartCriteria(minValue.Value, maxValue.Value);

            _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
            _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
            return newFilterCriteria;
        }

        public FilterCriteria AddAndUpdateCriteriaTypeSeven(Guid templateCriteriaId, Guid filterId, Guid filterGroupId, bool isReport, List<string> selectedItems)
        {
            TemplateCriteria templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            FilterCriteria filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteria.Id);
            int lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }
            else
            {
                FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, filterGroupId, isReport, templateCriteria, lastCountForFilterCriteria);

                newFilterCriteria.SelectedValue = selectedItems == null ? null : JsonConvert.SerializeObject(new SelectedValueSelectedItems
                {
                    SelectedItems = selectedItems
                });
                newFilterCriteria.SelectedValueInformation = selectedItems == null ? templateCriteria.Name : this.GetSelectedValueInformationForCriteriaType7(selectedItems);
                _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
                return newFilterCriteria;
            }
        }

        public FilterCriteria AddAndUpdateCriteriaTypeEight(Guid templateCriteriaId, Guid filterId, Guid filterGroupId, bool isReport, int? score)
        {
            TemplateCriteria templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            FilterCriteria filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteria.Id);
            int lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }
            else
            {
                FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, filterGroupId, isReport, templateCriteria, lastCountForFilterCriteria);

                newFilterCriteria.SelectedValue = score != null ? JsonConvert.SerializeObject(new SelectedValueScore
                {
                    Score = score.Value
                }) : null;
                newFilterCriteria.SelectedValueInformation = score != null ? this.GetSelectedValueInformationForCriteriaType8(score) : templateCriteria.Name;

                _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
                return newFilterCriteria;
            }
        }

        public FilterCriteria AddAndUpdateCriteriaTypeNine(Guid templateCriteriaId, Guid filterId, Guid filterGroupId, bool isReport, int? score)
        {
            TemplateCriteria templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            FilterCriteria filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteria.Id);
            int lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }
            else
            {
                FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, filterGroupId, isReport, templateCriteria, lastCountForFilterCriteria);

                newFilterCriteria.SelectedValue = score != null ? JsonConvert.SerializeObject(new SelectedValueScore
                {
                    Score = score.Value
                }) : null;
                newFilterCriteria.SelectedValueInformation = score != null ? GetSelectedValueInformationForCriteriaType9(score) : templateCriteria.Name;

                _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
                return newFilterCriteria;
            }
        }

        public FilterCriteria AddAndUpdateCriteriaTypeTen(Guid templateCriteriaId, Guid filterId, Guid filterGroupId, bool isReport /*, int? score*/)
        {
            TemplateCriteria templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            FilterCriteria filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteria.Id);
            int lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }
            else
            {
                FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, filterGroupId, isReport, templateCriteria, lastCountForFilterCriteria);
                newFilterCriteria.SelectedValue = null;
                newFilterCriteria.SelectedValueInformation = templateCriteria.Name;

                _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
                return newFilterCriteria;
            }
        }

        public FilterCriteria AddAndUpdateCriteriaType16(Guid templateCriteriaId, Guid filterId, Guid filterGroupId, bool isReport, int? minValue, int? maxValue)
        {
            var templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            var filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteria.Id);
            var lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }

            FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, filterGroupId, isReport, templateCriteria, lastCountForFilterCriteria);

            newFilterCriteria.SelectedValue = isReport || !templateCriteria.IsScreenable ? null : JsonConvert.SerializeObject(new SelectedValueMinMax
            {
                MinValue = minValue.Value,
                MaxValue = maxValue.Value
            });
            newFilterCriteria.SelectedValueInformation = isReport || !templateCriteria.IsScreenable ? templateCriteria.Name : GetSelectedValueInformationForChartCriteria(minValue.Value, maxValue.Value);

            _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
            _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
            return newFilterCriteria;
        }

        public FilterCriteria AddAndUpdateCriteriaType17(Guid templateCriteriaId, Guid filterId, Guid filterGroupId, bool isReport, int? score)
        {
            var templateCriteria = _companyScreeningRepository.GetTemplateCriteriaById(templateCriteriaId);
            var filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteria.Id);
            var lastCountForFilterCriteria = _companyScreeningRepository.GetLastCountForFilterCriteria(filterId);

            if (filterCriteria != null && isReport)
            {
                return filterCriteria;
            }

            FilterCriteria newFilterCriteria = GetFilterCriteriaFromTemplateCriteria(filterId, filterGroupId, isReport, templateCriteria, lastCountForFilterCriteria);

            newFilterCriteria.SelectedValue = score != null ? JsonConvert.SerializeObject(new SelectedValueScore { Score = score.Value }) : null;
            newFilterCriteria.SelectedValueInformation = score != null ? GetSelectedValueInformationForCriteriaType17(score) : templateCriteria.Name;

            _companyScreeningRepository.AddFilterCriteria(newFilterCriteria);
            _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(newFilterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.AddFilterCriteria);
            return newFilterCriteria;
        }

        private FilterCriteria GetFilterCriteriaFromTemplateCriteria(Guid filterId, Guid? filterGroupId, bool isReport, TemplateCriteria templateCriteria, int lastCountForFilterCriteria)
        {
            return new FilterCriteria
            {
                Id = Guid.NewGuid(),
                Name = templateCriteria.Name,
                FilterId = filterId,
                FilterGroupId = filterGroupId,
                TemplateCriteriaId = templateCriteria.Id,
                CriteriaLevel2Id = templateCriteria.CriteriaLevel2Id,
                CriteriaLevel3Id = templateCriteria.CriteriaLevel3Id,
                CriteriaEntity = templateCriteria.CriteriaEntity,
                CriteriaEntityField = templateCriteria.CriteriaEntityField,
                CriteriaType = templateCriteria.CriteriaType,
                IsDataPoint = isReport ? true : !templateCriteria.IsScreenable,
                Order = lastCountForFilterCriteria,
                ResearchProducts = templateCriteria.ResearchProducts
            };
        }


        #endregion

        #region Update filter criteria

        public FilterCriteria UpdateCriteriaTypeOne(Guid templateCriteriaId, Guid filterId, int minValue, int maxValue)
        {
            FilterCriteria filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteriaId);

            if (filterCriteria != null)
            {
                //update - deserialize SelectedValue
                filterCriteria.SelectedValue = JsonConvert.SerializeObject(new SelectedValueMinMax
                {
                    MinValue = minValue,
                    MaxValue = maxValue
                });
                filterCriteria.SelectedValueInformation = this.GetSelectedValueInformationForChartCriteria(minValue, maxValue);
                _companyScreeningRepository.UpdateFilterCriteria(filterCriteria);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(filterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.UpdateFilterCriteria);
                return filterCriteria;
            }
            else
                throw new Exception();
        }

        public FilterCriteria UpdateCriteriaTypeThree(Guid templateCriteriaId, Guid filterId, List<string> selectedItems)
        {
            FilterCriteria filterCriteria = _companyScreeningRepository.GetFilterCriteria(filterId, templateCriteriaId);

            if (filterCriteria != null)
            {
                //update
                var currentFilter = JsonConvert.DeserializeObject<CriteriaTypeThreeDto>(filterCriteria.SelectedValue);
                filterCriteria.SelectedValue = JsonConvert.SerializeObject(new
                {
                    SelectedItems = selectedItems
                });
                filterCriteria.SelectedValueInformation = this.GetSelectedValueInformationForCriteriaType3(selectedItems);
                _companyScreeningRepository.UpdateFilterCriteria(filterCriteria);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(filterCriteria), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.UpdateFilterCriteria);
                return filterCriteria;
            }
            else throw new Exception();
        }

        #endregion

        #region GetSelectedValueInformation

        public string GetSelectedValueInformationForChartCriteria(int minValue, int maxValue)
        {
            return "Min: " + minValue + " - Max: " + maxValue;
        }

        public string GetSelectedValueInformationForCriteriaType2(int? score)
        {
            switch (score)
            {
                case 1:
                    return "Category 5 involvement is restricted";

                case 2:
                    return "Category 5 and 4 involvement is restricted";

                case 3:
                    return "Category 5, 4 and 3 involvement is restricted";

                case 4:
                    return "Category 5, 4, 3 and 2 involvement is restricted";

                case 5:
                    return "Category 5, 4, 3, 2 and 1 involvement is restricted";

                default:
                    return string.Empty;
            }
        }

        public string GetSelectedValueInformationForCriteriaType3(List<string> SelectedItems)
        {
            return string.Join(", ", SelectedItems);
        }

        private string GetSelectedValueInformationForCriteriaType4(
            string categoryOfInvolvement, 
            bool involvement, 
            bool significantOwnership, 
            bool revenueRange, 
            double piRangeStart, 
            List<ResearchProduct> researchProducts)
        {
            if (researchProducts.Contains(ResearchProduct.Solutions))
                return $"Minimum revenue percent is set to: {piRangeStart}%";
            if (!revenueRange && involvement)
                return $"{categoryOfInvolvement}: Not involved.";
            if (!revenueRange && significantOwnership)
                return $"{categoryOfInvolvement}: Significant ownership.";
            if (revenueRange)
                return $"{categoryOfInvolvement}: {piRangeStart}% and above is restricted.";

            return string.Empty;
        }

        public string GetSelectedValueInformationForCriteriaType7(List<string> selectedItems)
        {
            StringBuilder information = new StringBuilder();
            foreach (var item in selectedItems)
            {
                if (item == "0")
                    information.Append("Compliant ");
                if (item == "1")
                    information.Append("Watchlist ");
                if (item == "2")
                    information.Append("Non-Compliant ");
            }

            return information.ToString().Trim().Replace(" ", " and ") + " companies are restricted.";
        }

        public string GetSelectedValueInformationForCriteriaType8(int? score)
        {
            switch (score)
            {
                case 5:
                    return "Category 5, 4, 3, 2 and 1 involvement is restricted";

                case 4:
                    return "Category 5, 4, 3 and 2 involvement is restricted";

                case 3:
                    return "Category 5, 4 and 3 involvement is restricted";

                case 2:
                    return "Category 5 and 4 involvement is restricted";

                case 1:
                    return "Category 5 involvement is restricted";

                default:
                    return string.Empty;
            }
        }

        public static string GetSelectedValueInformationForCriteriaType9(int? score)
        {
            if (score == null)
                return null;

            if (score == 10)
                return "Category 10 involvement is restricted";

            var restrictedCategories = new List<int>();
            for (var i = 10; i > score; i--)
            {
                restrictedCategories.Add(i);
            }
            return "Categories " + string.Join(", ", restrictedCategories) + " and " + score + " involvement is restricted";
        }

        public static string GetSelectedValueInformationForCriteriaType17(int? score)
        {
            if (score == null)
                return null;

            return "Minimum threshold is set to: " + score + ".";
        }

        public static string GetSelectedValueInformationForCriteriaType18(int? score)
        {
            if (score == null)
                return null;

            return "Minimum threshold is set to: " + score + ".";
        }

        #endregion

        public List<FilterCriteria> GetAllFilterCriteriaByFilterId(Guid filterId)
        {
            return _filterCriteriaRepository.GetAllFilterCriteriaByFilterId(filterId);
        }

        public List<FilterCriteria> GetFilterCriteriaGroupByFilterCriteriaId(Guid filterCriteriaId)
        {
            List<FilterCriteria> returnList = new List<FilterCriteria>();
            FilterCriteria filterCriteria = _filterCriteriaRepository.GetFilterCriteriaById(filterCriteriaId);
            if (filterCriteria != null)
            {
                if (filterCriteria.FilterGroupId != null)
                    return _filterCriteriaRepository.GetFilterCriteriaGroupByFilterGroupId(filterCriteria.FilterGroupId.Value);
                else
                {
                    returnList.Add(filterCriteria);
                    return returnList;
                }
            }
            return returnList;
        }

        #region DeleteFilterCriteria

        public void DeleteFilterCriteriaById(Guid filterCriteriaId)
        {
            string argsStr = filterCriteriaId.ToString();
            try
            {
                CompanyScreeningSerilog.Log.DeleteCriteria(argsStr);
                _filterCriteriaRepository.DeleteFilterCriteria(filterCriteriaId);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(filterCriteriaId), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.DeleteFilterCriteria);
                CompanyScreeningSerilog.Log.DeleteCriteriaSuccess(argsStr);
            }
            catch (Exception ex)
            {
                CompanyScreeningSerilog.Log.DeleteCriteriaError(argsStr, ex);
                throw;
            }
        }

        public void DeleteFilterCriteriaGroup(Guid filterId, Guid filterCriteriaId, Guid? filterGroupId)
        {
            var obj = new
            {
                filterId = filterId,
                filterCriteriaId = filterCriteriaId,
                filterGroupId = filterGroupId
            };
            string argsStr = obj.ToString();
            try
            {
                if (filterGroupId != null)
                {
                    this.DeleteFilterCriteriaByFilterGroupId(filterId, filterGroupId.Value);
                }
                else
                {
                    this.DeleteFilterCriteriaById(filterCriteriaId);
                }
            }
            catch (Exception ex)
            {
                CompanyScreeningSerilog.Log.DeleteCriteriaError(argsStr, ex);
                throw;
            }
        }

        public void DeleteFilterCriteriaByIdTemplateIdAndFilterId(Guid templateCriteriaId, Guid filterId)
        {
            var obj = new
            {
                templateCriteriaId = templateCriteriaId,
                filterId = filterId
            };

            string argsStr = obj.ToString();
            try
            {
                CompanyScreeningSerilog.Log.DeleteCriteria(argsStr);
                _filterCriteriaRepository.DeleteFilterCriteriaByIdTemplateIdAndFilterId(templateCriteriaId, filterId);
                Tuple<Guid, Guid> filterGuids = new Tuple<Guid, Guid>(templateCriteriaId, filterId);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(filterGuids), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.DeleteFilterCriteriaByIdTemplateIdAndFilterId);
                CompanyScreeningSerilog.Log.DeleteCriteriaSuccess(argsStr);
            }
            catch (Exception ex)
            {
                CompanyScreeningSerilog.Log.DeleteCriteriaError(argsStr, ex);
                throw;
            }
        }

        public void DeleteFilterCriteriaByFilterGroupId(Guid filterId, Guid filterGroupId)
        {
            string argsStr = filterGroupId.ToString();
            try
            {
                CompanyScreeningSerilog.Log.DeleteCriteria(argsStr);
                _filterCriteriaRepository.DeleteFilterCriteriaByFilterGroupId(filterId, filterGroupId);
                Tuple<Guid, Guid> filterGuids = new Tuple<Guid, Guid>(filterId, filterGroupId);
                _serviceBusPublisher.SendMessage(JsonConvert.SerializeObject(filterGuids), ReplicationUtils.EntityType.FilterCriteria, ReplicationUtils.Operation.DeleteFilterCriteriaByFilterGroupId);
                CompanyScreeningSerilog.Log.DeleteCriteriaSuccess(argsStr);
            }
            catch (Exception ex)
            {
                CompanyScreeningSerilog.Log.DeleteCriteriaError(argsStr, ex);
                throw;
            }
        }

        #endregion
    }
}
